package com.ruoyi.project.channel.channelScore.service;

import java.util.List;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.channel.channelScore.mapper.ChannelScoreMapper;
import com.ruoyi.project.channel.channelScore.domain.ChannelScore;
import com.ruoyi.project.channel.channelScore.service.IChannelScoreService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 跑分码商 服务层实现
 * 
 * @author CAIN
 * @date 2019-10-21
 */
@Service
public class ChannelScoreServiceImpl implements IChannelScoreService 
{
	@Autowired
	private ChannelScoreMapper channelScoreMapper;

	/**
     * 查询跑分码商信息
     * 
     * @param userId 跑分码商ID
     * @return 跑分码商信息
     */
    @Override
	public ChannelScore selectChannelScoreById(Long userId)
	{
	    return channelScoreMapper.selectChannelScoreById(userId);
	}
	
	/**
     * 查询跑分码商列表
     * 
     * @param channelScore 跑分码商信息
     * @return 跑分码商集合
     */
	@Override
	public List<ChannelScore> selectChannelScoreList(ChannelScore channelScore)
	{
	    return channelScoreMapper.selectChannelScoreList(channelScore);
	}
	
    /**
     * 新增跑分码商
     * 
     * @param channelScore 跑分码商信息
     * @return 结果
     */
	@Override
	public AjaxResult insertChannelScore(ChannelScore channelScore, User user)
	{
		// 获取父id 的费率
		if(1L != channelScore.getPid()){
			// 如果不是总代则费率不能超过上级代理的费率
			ChannelScore pchannelScore = selectChannelScoreById(channelScore.getPid());
			if(channelScore.getRate().compareTo(pchannelScore.getRate()) == 1){
				// 如果子代理费率大于父代理则抛出异常
				return AjaxResult.error("费率设置错误，子代理费率不能超过父代理");
			}
		}
		channelScore.setQq(channelScore.getQq());
		channelScore.setRate(channelScore.getRate());
		channelScore.setPid(channelScore.getPid());
	    channelScoreMapper.insertChannelScore(channelScore);
	    return AjaxResult.success("success");
	}
	
	/**
     * 修改跑分码商
     * 
     * @param channelScore 跑分码商信息
     * @return 结果
     */
	@Override
	public AjaxResult updateChannelScore(ChannelScore channelScore, User user)
	{
		ChannelScore _channelScore = selectChannelScoreById(user.getUserId());
		// 获取父id 的费率
		if(1L != _channelScore.getPid()){
			// 如果不是总代则费率不能超过上级代理的费率
			ChannelScore pchannelScore = selectChannelScoreById(_channelScore.getPid());
			if(_channelScore.getRate().compareTo(pchannelScore.getRate()) == 1){
				// 如果子代理费率大于父代理则抛出异常
				return AjaxResult.error("费率设置错误，子代理费率不能超过父代理");
			}
		}
		_channelScore.setQq(channelScore.getQq());
		_channelScore.setRate(channelScore.getRate());
	    channelScoreMapper.updateChannelScore(channelScore);
		return AjaxResult.success("操作成功");
	}

	/**
     * 删除跑分码商对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteChannelScoreByIds(String ids)
	{
		return channelScoreMapper.deleteChannelScoreByIds(Convert.toStrArray(ids));
	}
	
}
